<?php

class image_clip {

    static $tool;

    static function set_tool() {
        if (self::$tool)
            return;
        
        if (ECAE_MODE) {
            self::$tool = kernel::single('image_tools_ecae');
            return;
        }
        if (defined("IMAGE_TOOL") && in_array(IMAGE_TOOL, array(
            'ecae',
            'magickwand',
            'gd',
            'imagick'
        ))) {
            self::$tool = kernel::single('image_tools_' . IMAGE_TOOL);
        } else {
            self::$tool = kernel::single('image_tools_gd');
        }
    }

    static function image_resize(&$imgmdl, $src_file, $target_file, $new_width, $new_height) {
        self::set_tool();
        if (isset($src_file) && is_file($src_file)) {
            list ($width, $height, $type) = getimagesize($src_file);
            $size = self::get_image_size($new_width, $new_height, $width, $height);
            $new_width = $size[0];
            $new_height = $size[1];
            self::$tool->resize($src_file, $target_file, $width, $height, $type, $new_width, $new_height);
        }
    }

    static function get_image_size($new_width, $new_height, $org_width, $org_height) {
        $dest_width = $new_width;
        $dest_height = $new_height;
        if ($org_width > $org_height) {
            if ($org_width >= $new_width) {
                $dest_width = $new_width;
                $dest_height = round(($org_height / $org_width) * $new_width);
            }
        } else {
            if ($org_height >= $new_height) {
                $dest_height = $new_height;
                $dest_width = round(($org_width / $org_height) * $new_height);
            }
        }
        
        if (defined('WITHOUT_AUTOPADDINGIMAGE') && WITHOUT_AUTOPADDINGIMAGE) {
            
            if ($dest_width > $org_width) {
                $dest_width = $org_width;
            }
            
            if ($dest_height > $org_height) {
                $dest_height = $org_height;
            }
        }
        
        return array(
            $dest_width,
            $dest_height
        );
    }

    function image_watermark(&$imgmdl, $file, $set) {
        self::set_tool();
        switch ($set['wm_type']) {
            case 'text':
                $mark_image = $set['wm_text_image'];
                break;
            case 'image':
                $mark_image = $set['wm_image'];
                break;
            default:
                return;
        }
        if ($set['wm_text_preview']) {
            $mark_image = $set['wm_text_image'];
        } else {
            $mark_image = $imgmdl->fetch($mark_image);
        }
        
        list ($watermark_width, $watermark_height, $type) = getimagesize($mark_image);
        list ($src_width, $src_height) = getimagesize($file);
        list ($dest_x, $dest_y) = self::get_watermark_dest($src_width, $src_height, $watermark_width, $watermark_height, $set['wm_loc']);
        
        $set['watermark_width'] = $watermark_width;
        $set['watermark_height'] = $watermark_height;
        $set['type'] = $type;
        $set['src_width'] = $src_width;
        $set['src_height'] = $src_height;
        $set['dest_x'] = $dest_x;
        $set['dest_y'] = $dest_y;
        
        self::$tool->watermark($file, $mark_image, $set);
        
        // modified 2016-1-20 20:00 zengxinwen
        if (! is_array($mark_image) && strstr($mark_image, sys_get_temp_dir())) {
            @unlink($mark_image);
        }
    }

    static function get_watermark_dest($src_w, $src_h, $wm_w, $wm_h, $loc) {
        switch ($loc{0}) {
            case 't':
                $dest_y = ($src_h - 5 > $wm_h) ? 5 : 0;
                break;
            case 'm':
                $dest_y = floor(($src_h - $wm_h) / 2);
                break;
            default:
                $dest_y = ($src_h - 5 > $wm_h) ? ($src_h - $wm_h - 5) : 0;
        }
        
        switch ($loc{1}) {
            case 'l':
                $dest_x = ($src_w - 5 > $wm_w) ? 5 : 0;
                break;
            case 'c':
                $dest_x = floor(($src_w - $wm_w) / 2);
                break;
            default:
                $dest_x = ($src_w - 5 > $wm_w) ? ($src_w - $wm_w - 5) : 0;
        }
        
        return array(
            $dest_x,
            $dest_y
        );
    }
}
